# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division, absolute_import
import pyglet
from src import vars, entity, xtypes, cargo

# enumerate types
buildingtypes = xtypes.enumtype('BuildingTypes', 'general civil industrial')

class BuildingController(pyglet.event.EventDispatcher):
    """Building subsystem controller & Model controller.
    XXX
    XXX DO NOT FUCKING WANT. move the game subsystems controllers somewhere else.
    XXX
    """
    buildings = []
    def on_building_create(self, b):
        self.buildings.append(b)
        #if game.ctl:
        #    print "LOL game.ctl.dispatch_event('on_building_create', b)"
        # XXX
        # need send/receive events to games subsystems
    def on_building_select(self, b):
        pass
    def destroy(self):
        print self, 'DESTROYED'
        self.buildings.delete(b)
    def take_damage(self, amg):
        assert hasattr(self, 'health')
        self.health -= amg 
        print 'losing health', self, amg
        if self.health < 0:
            return self.destroy()
        if hasattr(self, 'on_health_change'):
            self.on_health_change()
    def get_all_buildings(self):
        return self.buildings
    def get_visible_buildings(self):
        return []
    def on_health_change(self):
        if self.health < 50:
            print '>>>', self, 'IS ON FIRE'

BuildingController.register_event_type('on_building_create')
BuildingController.register_event_type('on_building_select')
BuildingController.register_event_type('on_building_receive_cargo')
 
class BuildingModel(entity.StaticFieldEntity):
    _ctl = BuildingController()
    def __init__(self, cx=0, cy=0):
        super(BuildingModel, self).__init__(cx, cy)
        self.components = []
        self.cargo = xtypes.DotDict()

        # add view
        from src.views.building import BuildingView
        self.view = BuildingView(self)

        return self._ctl.dispatch_event('on_building_create', self)

         
class Building(BuildingController, BuildingModel):
    pass
